package com.accp.action;

import com.accp.biz.EmpBiz;
import com.accp.pojo.Emp;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class EmpAction implements ModelDriven<Emp> {
    private Emp emp = new Emp();
    @Override
    public Emp getModel() {
        return emp;
    }

    private EmpBiz empBiz;

    public void setEmpBiz(EmpBiz empBiz) {
        this.empBiz = empBiz;
    }

    /**
     * 分页查看员工信息
     *
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String list(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        try{
            List list = empBiz.fandAll("Emp", 1, 2,emp);
            int totalPage = empBiz.searchRecordsNextPage(2, "Emp",emp);
            request.put("empList", list);
            request.put("totalPage", totalPage);
            request.put("currentPage", 1);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "list";
    }
    /**
     * 分页查找下一页
     * @return
     */
    @SuppressWarnings("rawtypes")
    public String selectNextPage(){
        HttpServletRequest request = ServletActionContext.getRequest();
        int next = Integer.parseInt(request.getParameter("page"));
        try{
            Map<String,Object> session =  ActionContext.getContext().getSession();
            List list = empBiz.fandAll("Emp", next, 2,emp);
            int total = empBiz.searchRecordsNextPage(2, "Emp",emp);
            request.setAttribute("empList", list);
            request.setAttribute("currentPage", next);
            request.setAttribute("totalPage", total);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectNextPage";
    }

    /**
     * 删除一条员工信息
     */
    public String delete(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("id");
        Integer empId = Integer.parseInt(id);
        boolean flag = empBiz.deleteEmp(empId);
        if (flag){
            return "delete";
        }else{
            return "error";
        }
    }
    /**
     * 添加
     */
    @SuppressWarnings("unchecked")
    public String add(){
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        boolean flag = empBiz.add(emp);
        if(flag){
            request.put("message", "添加成功");
            return "add";
        }
        return "error";
    }
    /**
     * 查看一条员工信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public String byId(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("id");
        Integer empId = Integer.parseInt(id);
        Emp emp = empBiz.fandEmpById(empId);
        if(emp!=null){
            Map<String,Object> session = ActionContext.getContext().getSession();//获取seesion作用域对象。
            session.put("emp", emp);
            return "emp_update";
        }
        return "error";
    }
    /**
     * 修改
     */
    @SuppressWarnings("unchecked")
    public String update(){
        Map<String,Object> session = ActionContext.getContext().getSession();
        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        Emp emp1 = new Emp();
        emp1.setName(emp.getName());
        emp1.setSex(emp.getSex());
        emp1.setId(emp.getId());
        boolean flag = empBiz.update(emp1);
        if(flag){
            request.put("message", "修改成功");
            return "update";
        }
        return "error";
    }
}
